<?php
define('M_NOUSER',true);
include_once dirname(dirname(__FILE__)).'/include/general.inc.php';
include_once M_ROOT.'./include/common.fun.php';
if_siteclosed(1);
load_cache('fragments');
if(empty($frname) || empty($fragments[$frname])) mexit();
$fragment = &$fragments[$frname];
$tname = 'fr_'.$fragment['ename'];
$ttype = empty($fragment['tclass']) ? 'rtag' : 'ctag';
$frdata = empty($frdata) ? false : true;
$frview = empty($frview) ? false : true;
if(!$fragment['checked'] || ($fragment['startdate'] > $timestamp) || ($fragment['enddate'] && $fragment['enddate'] < $timestamp)) mexit();
if(!$frview && !$frdata && !pick_allow($fragment['hosts'])) mexit();
$_params = empty($fragment['params']) ? array() : array_filter(explode(',',$fragment['params']));
if($fragment['period']){
	$str = "frname=$frname";foreach($_params as $k) isset($$k) && $str .= "$k=".$$k;
	$cachefile = M_ROOT."./dynamic/fragment/$frname/".md5($str).".php";
	if(is_file($cachefile) && @filemtime($cachefile) > $timestamp - $fragment['period'] * 60){
		$_content = read_htmlcac($cachefile);
		mexit($frview || $frdata ? $_content : js_write($_content));
	}
}
$_charset = empty($charset) ? '' : trim($charset);
unset($charset);

$_content = '';
if($tag = read_tag($ttype,$tname)){
	$_da = array();
	foreach($_params as $k) isset($$k) && $_da[$k] = $$k;
	fr_refresh($tname,$ttype);
	@extract($btags);
	@extract($_da);
	_aenter($_da,1);
	ob_clean();
	$tplname = $ttype == 'ctag' ? $tname : $tag['template'];
	@include M_ROOT."template/$templatedir/pcache/$tplname.php";
	$_content = ob_get_contents();
	ob_clean();
}
if($_charset && $_content && $_charset != $mcharset){
	$_content = convert_encoding($mcharset,$_charset,$_content);
}
if($fragment['period']) save_htmlcac($_content,$cachefile);
mexit($frview || $frdata ? $_content : js_write($_content));
?>

